package Secondpagh;

import android.annotation.SuppressLint;
import android.app.Service;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import SHU.water2.R;

import static SHU.water2.MainActivity.globalj;
import static SHU.water2.MainActivity.globals;
import static SHU.water2.loginin.connection;

public class waterWarning extends AppCompatActivity implements View.OnClickListener {
    public ArrayList L1 = new ArrayList();
    public ArrayList L2 = new ArrayList();

    public static Vibrator vibrator;
    public static MediaPlayer alert;

    public boolean getdata = false;
    public List<HashMap<String, Object>> data;


    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.warning);
        ImmersionBar.with(this).statusBarColor(R.color.color).fitsSystemWindows(true).init();


        //震动
        long[] pattern = new long[]{200, 1000};
        vibrator = (Vibrator) getApplication().getSystemService(Service.VIBRATOR_SERVICE);
        if (vibrator.hasVibrator() && globalj == true) {
            vibrator.vibrate(pattern, 0);
        }

        //铃声
        alert = new MediaPlayer();
        alert = MediaPlayer.create(this, R.raw.alarm);
        alert.setLooping(true);
        if (globals == true) {
            alert.start();
        }


        click();
        connect();
    }

    private void connect() {
        //新建线程连接数据库，防止主界面卡死
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    String sql = "select * from site";
                    try {
                        // 创建用来执行sql语句的对象
                        Statement statement = connection.createStatement();
                        // 执行sql查询语句并获取查询信息
                        ResultSet rSet = statement.executeQuery(sql);

                        while (rSet.next()) {
                            L1.add(rSet.getString("name"));
                            //L2.add(rSet.getString("location"));
                        }


                        data = new ArrayList<HashMap<String, Object>>();

                        for (int i = 0; i < L1.size(); i++) {
                            HashMap<String, Object> item = new HashMap<String, Object>();
                            item.put("name", L1.get(i));
                            //item.put("location", L2.get(i));
                            data.add(item);
                        }


                        if (L1 != null)
                            getdata = true;

                    } catch (SQLException e) {
                        Log.e("site", "查询错误");
                    }


                } finally {
                }
            }
        }).start();
    }

    public void setview() {

        System.out.println(data);

        //创建SimpleAdapter适配器将数据绑定到item显示控件上
        SimpleAdapter adapter = new SimpleAdapter(waterWarning.this, data, R.layout.warninglist,
                new String[]{"name"}, new int[]{R.id.name});

        ListView listView1 = (ListView) findViewById(R.id.list1);
        //ListView listView2 = (ListView) findViewById(R.id.list2);
        //实现列表的显示
        listView1.setAdapter(adapter);
        //listView2.setAdapter(adapter);

    }

    private void click() {
        findViewById(R.id.ret1).setOnClickListener(this);
        findViewById(R.id.re1).setOnClickListener(this);
        findViewById(R.id.deal).setOnClickListener(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            vibrator.cancel();
            alert.stop();
            finish();
            return false;
        }
        return false;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ret1:

                vibrator.cancel();
                alert.stop();
                finish();
                break;

            case R.id.re1:
                if (getdata == false) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("尚未读回数据，请刷新重试").setPositiveButton("是", null).show();
                } else
                    setview();
                break;

            case R.id.deal:
                vibrator.cancel();
                alert.stop();
        }
    }
}
